window.addEventListener('load', function(){
    // 获取元素
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    var focusWidth = focus.offsetWidth;
    // 1、当经过轮播图的时候，左右箭头显示出来
    // 鼠标经过显示箭头
    focus.addEventListener('mouseenter', function(){
        arrow_r.style.display = 'block';
        arrow_l.style.display = 'block';
        // 鼠标放上去之后就停止动画播放
        // 因为timer不用了,最好清空
        clearInterval(timer);
        timer = null;
    })
    // 鼠标离开隐藏箭头
    focus.addEventListener('mouseleave', function(){
        arrow_r.style.display = 'none';
        arrow_l.style.display = 'none';
        // 11、鼠标离开,开启不需要重新var
        timer = setInterval(function(){
            arrow_r.click();
        }, 2000);
    })


    // 动态生成小圆圈
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.circle');
    var lis = ul.children;
    for (var i = 0; i < lis.length; i++){
        var li = document.createElement('li');
        ol.appendChild(li);
        li.setAttribute('index', i);
        // 点击小圆圈图片跟着改变，并变色
        li.addEventListener('click', function(){
            for (var i = 0; i < ol.children.length; i++){
                ol.children[i].className = '';
            }
            this.className = 'current';
            var index = this.getAttribute('index');
            num = index;
            circle = index;
            animate(ul, -index * focusWidth);
        })
    }
    ol.children[0].className = 'current';
    

    // 右键切换到下一张，到最后就变成第一张
    var flag = true;
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    var num = 0;
    var circle = 0;
    arrow_r.addEventListener('click', function(){
        if (flag){
            flag = false;
            if (num == ol.children.length){
                num = 0;
                ul.style.left = 0;
            }
            num ++;
            animate(ul, -num * focusWidth, function(){
                flag = true;
            });
            circle++;
            // 小圆圈跟着图片走
            if (circle == ol.children.length){
                circle = 0;
            }
            circleChange();
        }
    })
    

    // 点击左侧到下一张，到第一张就变成最后一张
    arrow_l.addEventListener('click', function(){
        if (flag){
            flag = false;
            if (num == 0){
                num = ol.children.length;
                // 加px！！！！！！
                ul.style.left = -num * focusWidth + 'px';
            }
            num --;
            animate(ul, -num * focusWidth, function(){
                flag = true;
            });
            // 小圆圈跟着图片走
            if (circle == 0){
                circle = ol.children.length;
            }
            circle--;
            circleChange();
        }
    })
    // 封装的函数
    function circleChange(){
        for (var i = 0; i < ol.children.length; i++){
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current';
    }


    // 自动播放图片
    var timer = setInterval(function(){
        arrow_r.click();
    }, 2000);
})
